<template>
  <VDialog @close="handleClose" :loading="loading" @save="save">
    <template #content>
      <div>
        <el-form
          ref="formRef"
          :model="form"
          :rules="rules"
          label-width="80"
          label-position="top"
        >
          <el-form-item>
            <div>
              <span class="mr-[8px]">已选择:</span>
              <span class="text-[#e50517] mr-[8px]">{{ ids?.length || 0 }}</span
              >数据
            </div>
          </el-form-item>
          <el-form-item label="审核" prop="state">
            <el-radio-group v-model="form.state">
              <el-radio
                :label="item.value"
                v-for="item in option"
                :key="item"
                >{{ item.label }}</el-radio
              >
            </el-radio-group>
          </el-form-item>
          <el-form-item label="利息金额" prop="interestAmount">
            <el-input-number
              v-model="form.interestAmount"
              placeholder="请输入"
              :controls="false"
              class="w-[100%]"
            />
          </el-form-item>
        </el-form>
      </div>
    </template>
  </VDialog>
</template>
<script setup>
import { ElMessage } from "element-plus";
import { reviewAcceptanceBill } from "../../../../api.js";
const props = defineProps({
  ids: { type: Array, default: () => [] },
});
const option = [
  { label: "待审核", value: 0 },
  { label: "待承兑", value: 1 },
  { label: "已承兑", value: 2 },
];
const emit = defineEmits(["update:modelValue", "close", "refresh"]);
const loading = ref(false);
const { ids } = toRefs(props);
const form = ref({});
const formRef = ref();
const rules = reactive({
  state: [{ required: true, message: "请选择审核状态", trigger: "change" }],
  interestAmount: [
    { required: true, message: "请输入利息金额", trigger: "blur" },
  ],
});
const save = async () => {
  await formRef.value.validate(async (valid, fields) => {
    if (valid) {
      try {
        loading.value = true;
        const { code, message } = await reviewAcceptanceBill({
          ids:ids.value,
          ...form.value,
        });
        if (code !== 200) return ElMessage.error(message);
        ElMessage.success("审核成功!");
        handleClose(true);
      } catch (error) {
      } finally {
        loading.value = false;
      }
    }
  });
};
const handleClose = async (refresh) => {
  form.value = {};
  await nextTick();
  formRef.value.resetFields();
  emit("update:modelValue", false);
  emit("close");
  if (refresh) emit("refresh");
};
defineExpose({ open });
</script>
